package com.ruoyi.system.billiards.weixin;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.billiards.common.GameStatusEnum;
import com.ruoyi.system.billiards.domian.request.EightBallScoreReq;
import com.ruoyi.system.billiards.domian.response.GameInfo;
import com.ruoyi.system.domain.BilliardsGame;
import com.ruoyi.system.domain.vo.BilliardsGameVo;
import com.ruoyi.system.service.IBilliardsGameService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 桌球比赛Controller
 *
 * @author ruoyi
 * @date 2024-12-11
 */
@RestController
@RequestMapping("/wx/billiards/game")
public class BilliardsGameController extends BaseController {
    @Autowired
    private IBilliardsGameService billiardsGameService;

    @GetMapping
    public AjaxResult list(String status) {
        List<BilliardsGameVo> newBilliardsGames = billiardsGameService.findByStatus(status);
        return AjaxResult.success(newBilliardsGames);
    }

    /**
     * 新增桌球比赛
     */
    @PostMapping
    public AjaxResult create(@RequestBody BilliardsGame billiardsGame) {
        if (StringUtils.isBlank(billiardsGame.getCreateBy())) {
            throw new IllegalArgumentException("用户不能为空");
        }
        if (StringUtils.isBlank(billiardsGame.getGameType())) {
            throw new IllegalArgumentException("比赛类型不能为空");
        }
        BilliardsGame game = billiardsGameService.selectCurGame(Long.valueOf(billiardsGame.getCreateBy()), billiardsGame.getGameType());
        if (game != null) {
            throw new IllegalArgumentException("尚有未结束的比赛");
        }
        BilliardsGame newBilliardsGame = billiardsGameService.insertBilliardsGame(billiardsGame);
        return AjaxResult.success(newBilliardsGame);
    }

    @PutMapping("over/{gameId}")
    public AjaxResult create(@PathVariable("gameId") Long gameId) {
        if (gameId == null) {
            throw new IllegalArgumentException("比赛id不能为空");
        }
        billiardsGameService.end(gameId);
        return AjaxResult.success();
    }

    @PutMapping("back/{gameId}")
    public AjaxResult back(@PathVariable("gameId") Long gameId) {
        if (gameId == null) {
            throw new IllegalArgumentException("比赛id不能为空");
        }
        billiardsGameService.back(gameId);
        return AjaxResult.success();
    }

    @PostMapping("score")
    public AjaxResult score(@RequestBody EightBallScoreReq eightBallScoreReq) {
        if (StringUtils.isAnyBlank(eightBallScoreReq.getTag(), eightBallScoreReq.getScoreMethod())) {
            throw new IllegalArgumentException("参数错误");
        }
        if (eightBallScoreReq.getGameId() == null) {
            throw new IllegalArgumentException("参数错误");
        }
        billiardsGameService.score(eightBallScoreReq);
        return AjaxResult.success();
    }

    /**
     * 查询当前进行中的比赛
     */
    @GetMapping("start")
    public AjaxResult myCurGame(Long memberId, String gameType) {
        if (memberId == null) {
            throw new IllegalArgumentException("参数错误");
        }
        return AjaxResult.success(billiardsGameService.selectCurGame(memberId, gameType));
    }

    /**
     * 加入比赛
     */
    @PutMapping("join")
    public AjaxResult joinGame(Long gameId, Long memberId) {
        billiardsGameService.joinGame(gameId, memberId);
        return AjaxResult.success();
    }

    @GetMapping("/{gameId}")
    public AjaxResult getInfo(@PathVariable("gameId") Long gameId) {
        if (gameId == null) {
            throw new IllegalArgumentException("参数错误");
        }
        GameInfo gameInfo = billiardsGameService.gameDetail(gameId);
        return AjaxResult.success(gameInfo);
    }

    @GetMapping("my/{memberId}")
    public AjaxResult my(@PathVariable("memberId") Long memberId) {
        List<BilliardsGameVo> gameVoList = billiardsGameService.myGames(memberId);
        return AjaxResult.success(gameVoList);
    }
}
